package mywork

import akka.actor.{Actor, ActorLogging}
import akka.cluster.singleton.{ClusterSingletonProxy, ClusterSingletonProxySettings}
import akka.pattern._
import akka.util.Timeout
import scala.concurrent.duration._

/**
  * Created by shichen on 2017/8/1.
  */
object MyFrontend {
  case object Ok
  case object NotOk
}

class MyFrontend extends Actor with ActorLogging {
  import context.dispatcher

  val masterProxy = context.actorOf(
    ClusterSingletonProxy.props(
      settings = ClusterSingletonProxySettings(context.system)
        .withRole("backend"),
      singletonManagerPath = "/user/master"
    ),
    name = "masterproxy")

  override def receive: Receive = {
    case work =>
      implicit val timeout = Timeout(5.seconds) //不能写在case上面
      (masterProxy ? work) map {
        case MyMaster.Ack(_) => MyFrontend.Ok
      } recover { case _ => MyFrontend.NotOk} pipeTo sender()
  }
}
